<?php
namespace app\common\model;

use think\Log;
use think\Model;

/**
 * 模型
 * Class AdminUser
 * @package app\common\model
 */
class CheckoutHelp extends Model
{


    /**
     * 查询一条信息
     *
     * @param array $params
     * @param bool $fields
     * @return array
     */
    public function getInfo(array $params, $fields = true)
    {
        $where = $return = [];

        if (isset($params['id']) && $params['id'] > 0) {
            $where['id'] = $params['id'];
        }

        if (isset($params['is_calcel']) && is_numeric($params['is_calcel'])) {
            $where['is_calcel'] = $params['is_calcel'];
        }

        if (!isset($params['add_time'])) {
            //只获取2小时内发起的支援请求
            $where['add_time'] = ['egt', date('Y-m-d H:i:s', time() - 2*3600)];
        }

        if (isset($params['order_str'])) {
            $order = $params['order_str'];
        } else {
            $order = 'id desc';
        }

        if (!empty($where)) {
            $res = $this->where($where)->field($fields)->order($order)->find();
            if (!empty($res)) {
                $return = $res->toArray();
            }
        }

        return $return;
    }

    /**
     * 查询一条信息
     *
     * @param array $params
     * @param bool $fields
     * @param string $order 指定排序方式
     * @return array
     */
    public function getInfoByOrder(array $params, $fields = true, $order = '')
    {
        $where = $return = [];

        if (isset($params['id']) && $params['id'] > 0) {
            $where['id'] = $params['id'];
        }

        if (isset($params['asset_id']) && $params['asset_id'] > 0) {
            $where['asset_id'] = $params['asset_id'];
        }

        if (!empty($where)) {
            if (empty($order)) {
                $order = 'id desc';
            }

            $res = $this->where($where)->field($fields)->order($order)->find();
            if (!empty($res)) {
                $return = $res->toArray();
            }
        }

        return $return;
    }

    /**
     * 修改数据
     *
     * @param $id
     * @param array $req
     * @return bool | integer
     */
    public function saveDate($id, array $req)
    {
        if ($id <= 0) {
            return false;
        }

        $data = $req;
        $data['modify_time'] = NOW_DATE;

        $res = $this->allowField(true)->save($data, ['id' => $id]);
        if ($res === false || $res > 1) {
            Log::record('更新检验数据失败：' . $this->getLastSql());
            return false;
        }

        // 清空原始数据，防止上一条对下一条的影响
        $this->origin = [];

        return $res;
    }

    /**
     * 修改多条数据
     *
     * @param array $params
     * @param array $req
     * @return bool | integer
     */
    public function saveDateList(array $params, array $req)
    {
        if (empty($params) || empty($req)) {
            return false;
        }

        $where = [];
        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        $data = $req;
        $data['modify_time'] = NOW_DATE;

        $res = $this->allowField(true)->save($data, $where);

        if ($res === false) {
            \think\Log::record('删除数据失败:' . $this->getLastSql());
        }

        return $res;
    }


    /**
     * 添加一条数据
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $data
     * @return  integer|boolean
     */
    public function addOne(array $data)
    {
        $asset_data = $data;
        $asset_data['add_time'] = NOW_DATE;

        $res = $this->allowField(true)->data($asset_data,true)->isUpdate(false)->save();
        $return = ($res && isset($this->id) && $this->id > 0) ? $this->id : false;
        if ($return === false) {
            Log::record('添加支援请求记录失败!');
        }
        return $return;
    }

    /**
     * 查询列表
     *
     * @author mayy
     * @date 2017-8-15
     * @param array $params
     * @param $limit
     * @param bool $fields
     * @param bool $is_count
     * @return array
     */
    public function getList(array $params, $limit = 20, $is_count = false, $fields = true)
    {
        $where = $return = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (isset($params['is_calcel']) && is_numeric($params['is_calcel'])) {
            $where['is_calcel'] = $params['is_calcel'];
        }

        if (isset($params['operator_id']) && $params['operator_id'] > 0) {
            $where['operator_id'] = $params['operator_id'];
        }

        if (!isset($params['add_time'])) {
            //只获取2小时内发起的支援请求
            $where['add_time'] = ['egt', date('Y-m-d H:i:s', time() - 2*3600)];
        }

        if (isset($where['order_str'])) {
            $order = $where['order_str'];
        } else {
            $order = 'id desc';
        }

        $count = $this->where($where)->count();
        if ($is_count) {
            return $count;
        }

        $data = $this->where($where)->field($fields)->order($order)->paginate($limit);

        return $data->toArray();
    }


    /**
     * 删除指定记录
     *
     * @param array $params
     * @return false | integer
     */
    public function delData(array $params)
    {
        if (empty($params)) {
            return false;
        }

        $where = [];

        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $where['id'] = ['in', $params['id']];
            } else {
                $where['id'] = $params['id'];
            }
        }

        if (empty($where)) {
            return false;
        }

        $res = $this->where($where)->delete();
        if ($res === false) {
            \think\Log::record('删除数据失败:' . $this->getLastSql());
        }
        return $res;
    }

    /**
     * 根据id删除一条资产
     *
     * @param $asset_id
     * @return bool
     */
    public function delOneById($asset_id)
    {
        if ($asset_id <= 0) {
            return false;
        }

        $res = $this->where(['id' => $asset_id])->limit(1)->delete();
        if ($res === false) {
            \think\Log::record('删除数据失败:' . $this->getLastSql());
        }

        return $res;
    }

}